package com.itheima.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.UmsMenu;
import com.itheima.service.UmsMenuService;
import com.itheima.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class UmsMenuManager {

    @DubboReference
    private UmsMenuService umsMenuService;



    // 菜单分页查询
    public ResultData getListByPage(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<UmsMenu> listByPage = umsMenuService.getListByPage(pageNum, pageSize, parentId);
        return ResultData.ok(new PageResult(pageNum,pageSize,listByPage.getTotal(),listByPage.getPages(),listByPage.getRecords()),"");
    }

    // 添加后台菜单
    public ResultData create(UmsMenu umsMenu) {
       // umsMenu.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        if(umsMenu.getParentId() == 0){
            umsMenu.setLevel(0);
        }else {
            umsMenu.setLevel(1);
        }
        Integer count = umsMenuService.create(umsMenu);
        return ResultData.ok(count,"添加成功");
    }

    // 删除后台菜单
    public ResultData delete(Integer id) {
        Integer count = umsMenuService.delete(id);
        return ResultData.ok(count,"删除成功");
    }

    // 修改菜单显示状态
    public ResultData updateHidden(Integer id, Integer hidden) {
        Integer count = umsMenuService.updateHiddenById(id, hidden);
        return ResultData.ok(count,"修改成功");
    }

    // 根据ID获取菜单详情
    public ResultData getById(Integer id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return ResultData.ok(umsMenu,"");
    }

    // 修改后台菜单
    public ResultData update( UmsMenu umsMenu) {
        if(umsMenu.getParentId() == 0){
            umsMenu.setLevel(0);
        }else {
            umsMenu.setLevel(1);
        }
        Integer count = umsMenuService.update(umsMenu);
        return ResultData.ok(count,"修改成功");
    }

    // 树形结构返回所有菜单列表
    public ResultData treeList() {

        // 1. 查询所有菜单列表
        List<UmsMenu> allMenu = umsMenuService.getAll();
        // 2. 定义根节点菜单集合
        List<UmsMenuVo> rootMenus = new ArrayList<>();
        for (UmsMenu umsMenu : allMenu) {
            // 2.1 判断 parentId 是否为 0, 0 则是跟节点
            if(umsMenu.getParentId() == 0){
                UmsMenuVo umsMenuVo = new UmsMenuVo();
                BeanUtils.copyProperties(umsMenu, umsMenuVo);
                rootMenus.add(umsMenuVo);
            }
        }
        // 3. 遍历跟节点,为其添加子节点 children
        for (UmsMenuVo menu : rootMenus) {
            List<UmsMenuVo> children = getChild(menu.getId(), allMenu);
            menu.setChildren(children);
        }
        return ResultData.ok(rootMenus,"");
    }

    // 获取子节点
    private List<UmsMenuVo> getChild(Long id, List<UmsMenu> allMenu) {
        // 定义子节点集合
        List<UmsMenuVo> childList = new ArrayList<>();
        for (UmsMenu menu : allMenu) {
            // 如果 传来的 id 和 parentId 一样,则添加进 childList 中
            if (id.equals(menu.getParentId())) {
                UmsMenuVo menuDto = new UmsMenuVo();
                BeanUtils.copyProperties(menu, menuDto);
                childList.add(menuDto);
            }
        }

        // 如果 childList 大小为0,说明没有子节点,则返回,结束递归
        if (childList.size() == 0) {
            return childList;
        }

        // 不为0,递归
        for (UmsMenuVo umsMenuVo : childList) {
            umsMenuVo.setChildren(getChild(umsMenuVo.getId(), allMenu));
        }
        return childList;
    }
}
